package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("DetectmethodController")
public class DetectmethodController extends CmiEmaController {

	@RequestMapping("/listDetectmethods")
	public ModelAndView listDetectmethods(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = detectmethodPaginationService.updatePagination(request, pagination);
		Set<Detectmethod> detectmethods = detectmethodPaginationService.getPaginationDetectmethods(pagination);

		mav.addObject("detectmethods", detectmethods);
		mav.addObject("entityName", "detectmethod");
		mav.addObject("pagination", pagination);
		mav.setViewName("detectmethod/listDetectmethods.jsp");
		return mav;
	}

	public String listDetectmethods() {
		return "redirect:/listDetectmethods";
	}

	@RequestMapping("/viewDetectmethod")
	public ModelAndView viewDetectmethod(@RequestParam Integer detectmethodId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("detectmethod", detectmethodDAO.findDetectmethodByPrimaryKey(detectmethodId));
		mav.setViewName("detectmethod/viewDetectmethod.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDetectmethods")
	public ModelAndView confirmDeleteDetectmethods(
			@RequestParam(value = "deleteDetectmethodsIds", required = true) Set<Integer> deleteDetectmethodsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Detectmethod> deleteDetectmethods = detectmethodDAO.findDetectmethodsByIds(deleteDetectmethodsIds);
		Set<Detectmethod> referencedDetectmethods = detectmethodService.loadReferenceDetectmethods(deleteDetectmethods);
		boolean hasReferenced;
		if (referencedDetectmethods.size() == 0) {
			mav.addObject("deleteEntitys", deleteDetectmethods);
			hasReferenced = false;
		} else {
			deleteDetectmethods.removeAll(referencedDetectmethods);
			mav.addObject("referencedEntitys", referencedDetectmethods);
			mav.addObject("deleteEntitys", deleteDetectmethods);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Detectmethod");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDetectmethods");
		return mav;
	}

	@RequestMapping("/deleteDetectmethod")
	public String deleteDetectmethod(@RequestParam Integer detectmethodId) {
		Detectmethod detectmethod = detectmethodDAO.findDetectmethodByPrimaryKey(detectmethodId);
		detectmethodService.deleteDetectmethod(detectmethod);
		return "forward:/listDetectmethods";
	}

	@RequestMapping("/confirmDeleteDetectmethod")
	public ModelAndView confirmDeleteDetectmethod(@RequestParam Integer detectmethodId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("detectmethod", detectmethodDAO.findDetectmethodByPrimaryKey(detectmethodId));

		mav.setViewName("detectmethod/deleteDetectmethod.jsp");

		return mav;
	}

	@RequestMapping("/deleteDetectmethods")
	public String deleteDetectmethods(
			@RequestParam(value = "deleteDetectmethodsId", required = true) Set<Integer> deleteDetectmethodsId) {
		detectmethodService.deleteDetectmethods(deleteDetectmethodsId);
		return "forward:/listDetectmethods";
	}

	@RequestMapping("/editDetectmethod")
	public ModelAndView editDetectmethod(@RequestParam Integer detectmethodId) {

		ModelAndView mav = new ModelAndView();

		Detectmethod detectmethod = detectmethodDAO.findDetectmethodByPrimaryKey(detectmethodId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("detectmethod", detectmethod);

		// 把所有的引用detectmethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectmethodService.loadDetectmethodObjectReferencedSet(detectmethod));

		// 将编辑页面detectmethod添加到mav中
		mav.setViewName("detectmethod/editDetectmethod.jsp");
		return mav;
	}

	@RequestMapping("/newDetectmethod")
	public ModelAndView newDetectmethod() {
		ModelAndView mav = new ModelAndView();
		Detectmethod detectmethod = new Detectmethod();

		mav.addObject("detectmethod", detectmethod);

		// 把所有的引用detectmethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(detectmethodService.loadDetectmethodObjectReferencedSet(detectmethod));

		mav.addObject("newFlag", true);
		mav.setViewName("detectmethod/editDetectmethod.jsp");

		return mav;
	}

	@RequestMapping("/saveDetectmethod")
	public ModelAndView saveDetectmethod(@ModelAttribute Detectmethod detectmethod) {

		ModelAndView mav = new ModelAndView();

		Detectmethod existingDetectmethod = CmiHibernateProxyUtil
				.deproxy(detectmethodDAO.findDetectmethodByPhysicalPrimaryKey(detectmethod.getName()));

		// 新建记录
		if (detectmethod.getId() == null) {
			if (detectmethod.equals(existingDetectmethod)) {

				mav.addAllObjects(detectmethodService.loadDetectmethodClassReferencedSet(detectmethod));
				// mav = editContinue(mav, detectmethod);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectmethod/editDetectmethod.jsp");
			} else {
				detectmethodService.saveDetectmethod(detectmethod);
				mav.setViewName("forward:/listDetectmethods");
			}
		}
		// 编辑记录
		else {
			Detectmethod originDetectmethod = CmiHibernateProxyUtil
					.deproxy(detectmethodDAO.findDetectmethodByPrimaryKey(detectmethod.getId()));
			if (!detectmethod.equals(originDetectmethod) && detectmethod.equals(existingDetectmethod)) {
				mav.addAllObjects(detectmethodService.loadDetectmethodClassReferencedSet(detectmethod));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("detectmethod/editDetectmethod.jsp");
			} else {
				detectmethodService.saveDetectmethod(detectmethod);
				mav.setViewName("forward:/listDetectmethods");
			}
		}
		return mav;
	}

	@RequestMapping("/detectmethodController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
